package com.iyunsir.lesson01_tcp;


import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;


//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;

        // idea try - catch 的快捷键 “Ctrl + Alt + t ”

        try {
            // 1.要知道服务器地址
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");

            // 2.端口号
            int port = 9999;
            socket = new Socket(serverIp,port);

            // 3.发送消息
            os = socket.getOutputStream();
            os.write("你好".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
